package com.itheima.controller;

import com.itheima.pojo.PageBean;
import com.itheima.pojo.Pet;
import com.itheima.pojo.Result;
import com.itheima.service.PetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    private PetService petService;
    @PostMapping
    public Result add(@RequestBody @Validated(Pet.Add.class) Pet pet){
        petService.add(pet);
        return Result.success();
    }
    @DeleteMapping
    public Result<Pet> delete(Pet pet){
        petService.delete(pet);
        return Result.success();
    }
    //查询登录用户的宠物信息
    @GetMapping
    public Result<List<Pet>> list(){
        List<Pet> ps = petService.list();
        return Result.success(ps);
    }
    //查询所有宠物信息
    @GetMapping("/all")
    public Result<PageBean<Pet>> all(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) String createUser
    ){
        PageBean<Pet> pb = petService.all(pageNum,pageSize,createUser);
        return Result.success(pb);
    }
//    public Result<List<Pet>> all(){
//        List<Pet> ps = petService.all();
//        return Result.success(ps);
//    }
    @GetMapping("/detail")
    public Result<Pet> detail(Integer id){
        Pet c = petService.findById(id);
        return Result.success(c);
    }
    @PutMapping
    public Result update(@RequestBody @Validated(Pet.Update.class) Pet pet){
        petService.update(pet);
        return Result.success();
    }
}
